package com.bocai.yuweiwangluo.presenter;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.bocai.yuweiwangluo.activity.SetNewPsdSuccessActivity;
import com.bocai.yuweiwangluo.base.BaseView;
import com.bocai.yuweiwangluo.bean.BaseBean;
import com.bocai.yuweiwangluo.bean.RegistBean;
import com.bocai.yuweiwangluo.net.MyOkHttpClient;
import com.bocai.yuweiwangluo.utils.UrlData;
import com.bocai.yuweiwangluo.viewModel.IChangePwdView;
import com.google.gson.Gson;

/**
 * Created by admin on 2015/12/16.
 */
public class ChangePwdPresenter {
    private RegistBean registBean;
    private BaseBean bean;
    private static final int RESPONSE_SUCCEED = 1;
    private static final int RESPONSE_FAILED = 2;
    private IChangePwdView changePwdView;
    private Context context;
    private int requsttype;
    private int delaytime;
    private Button getCodebtn;
    private BaseView baseView;
    private MyOkHttpClient myOkHttpClient;
    int code;
    public ChangePwdPresenter(Context context, IChangePwdView changePwdView,BaseView baseView,Button getCodebtn) {
        myOkHttpClient = new MyOkHttpClient(context,baseView);
        this.context = context;
        this.changePwdView = changePwdView;
        this.baseView=baseView;
        this.getCodebtn=getCodebtn;
    }
    /**
     * 手机端随机生成4位验证码，发送给服务器，服务器将其保存在数据库中
     */
    public void getCode()
    {
        if ("".equals(changePwdView.getTel()))
        {
            Toast.makeText(context,"请输入手机号",Toast.LENGTH_SHORT).show();
        }
        if (changePwdView.getTel().length() != 11)
        {
            Toast.makeText(context, "手机号错误", Toast.LENGTH_SHORT).show();
        } else
        {
            requsttype = 1;
            myOkHttpClient.getCode(UrlData.BASEURL + UrlData.GETCODE, changePwdView.getTel(),handler);
        }
    }
    /**
     * 将随机生成的验证码通过短信的形式发送到用户的手机上
     * @param code
     */
    private void sendMsm(String code)
    {
        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(changePwdView.getTel(), null, code, null, null);
    }
    /**
     * 验证码验证
     */
    public void verificationCode()
    {

        requsttype=2;
        if("".equals(changePwdView.getTel()))
        {
            Toast.makeText(context,"请输入手机号",Toast.LENGTH_SHORT).show();
        }
        else if (changePwdView.getTel().length()!= 11)
        {
            Toast.makeText(context, "手机号错误", Toast.LENGTH_SHORT).show();
        }
        else if ("".equals(changePwdView.getCode()))
        {
            Toast.makeText(context, "请输入验证码", Toast.LENGTH_SHORT).show();
        }

        else
        {
            baseView.showLoading();
            myOkHttpClient.verificationCode(UrlData.BASEURL + UrlData.VERIFICATIONCODE, changePwdView.getTel(), changePwdView.getCode(),handler);
        }
    }
    public void changePwd(){
        requsttype = 3;
        if("".equals(changePwdView.getPwd())){
            Toast.makeText(context, "请输入密码", Toast.LENGTH_SHORT).show();
        }
        if(changePwdView.getPwd().length()<6){
            Toast.makeText(context, "您设置的密码不足6位", Toast.LENGTH_SHORT).show();
        }
        else  if("".equals(changePwdView.getPwdtrue()))
        {
            Toast.makeText(context,"请确认密码",Toast.LENGTH_SHORT).show();
        }else if(!changePwdView.getPwd().equals(changePwdView.getPwdtrue())){
            Toast.makeText(context,"两次输入的密码不一致",Toast.LENGTH_SHORT).show();
        }else{
            baseView.showLoading();
            myOkHttpClient.forgetPwd(UrlData.BASEURL + UrlData.FORGETPWD, changePwdView.getTel(),changePwdView.getPwd(), handler);
        }
    }
        private Handler handler = new Handler()
        {
            @Override
            public void handleMessage(Message msg)
            {
                super.handleMessage(msg);
                String result = (String) msg.obj;
                Gson gson = new Gson();
                switch (msg.what)
                {
                    case RESPONSE_SUCCEED:
                        Log.e("tag", result);
                        bean = gson.fromJson(result, BaseBean.class);
                        if (requsttype == 1)//发送验证码
                        {
                            if (0 == bean.getState())
                            {
                                try {
                                    Toast.makeText(context,"验证码已发送至手机",Toast.LENGTH_SHORT).show();
                                    delaytime = 60;
                                    getCodebtn.setClickable(false);
                                    new Thread(new ThreadShow()).start();
                                } catch (Exception e)
                                {
                                }
                            } else
                            {
                                Toast.makeText(context,bean.getMsg(), Toast.LENGTH_SHORT).show();
                            }
                        } else if (requsttype == 2)//验证验证码
                        {
                            baseView.hideLoading();
                            if (0 == bean.getState())
                            {
                                //验证验证码成功，隐藏获取验证码界面，显示填写密码界面
                                changePwdView.getLLGetCode().setVisibility(View.GONE);
                                changePwdView.getLLChangepwd().setVisibility(View.VISIBLE);

                            } else {
                                Toast.makeText(context, "验证码错误", Toast.LENGTH_SHORT).show();
                            }
                        } else if (requsttype == 3)
                        {
                            baseView.hideLoading();
                            //设置新密码
                            if (0 == bean.getState())
                            {
                                SharedPreferences sharedPreferences = context.getSharedPreferences("user", Context.MODE_PRIVATE);
                                SharedPreferences.Editor editor = sharedPreferences.edit();
                                editor.putString("password", changePwdView.getPwd());
                                editor.commit();
                                Intent intent=new Intent(context, SetNewPsdSuccessActivity.class);
                                context.startActivity(intent);
                                changePwdView.finishActivity();
                            } else
                            {
                                Toast.makeText(context, bean.getMsg(), Toast.LENGTH_SHORT).show();
                            }
                        }

                        break;
                    case RESPONSE_FAILED:
                        baseView.hideLoading();
                        baseView.showError(result);
                        break;
                    case 88:
                        getCodebtn.setText(delaytime + "s");
                        if (delaytime == 0)
                        {
                            getCodebtn.setClickable(true);
                            getCodebtn.setText("再次获取");
                        }
                        break;
                }
            }
    };
    // 时间倒计时
    class ThreadShow implements Runnable {
        @Override
        public void run() {
            // TODO Auto-generated method stub
            while (delaytime > 0) {
                try {
                    Thread.sleep(1000);
                    delaytime--;
                    Message msg = new Message();
                    msg.what = 88;
                    handler.sendMessage(msg);
                } catch (Exception e) {
                }
            }
        }
    }
}